package com.saatcioglu.android.guessthenumber;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class GfxTutorial extends GfxGuesstheNumber {
	
	private int intTutorialCounter = 0;
	private int intSecretNumber = 758;
	private int[] arrShots = {
			0,
			123,
			0,
			456,
			0,
			412,
			152,
			0,
			789,
			0,
			0,
			759,
			0,
			0,
			859,
			0,
			0,
			758
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.d("GfxTutorial::onCreate", Integer.toString(SMBGuesstheNumber.nActiveDigits));
		SMBGuesstheNumber.nActiveDigits = 3;
		SMBGuesstheNumber.nActiveMaxShots = 0;
		SMBGuesstheNumber.nActiveMaxTime = 0;
		SMBGuesstheNumber.isGameStarted = false;
		super.onCreate(savedInstanceState);
		showDialog(R.id.display_start);
	}

	@Override
	protected void StartaNewGame() {
		super.StartaNewGame();
		CtrlGuesstheNumber.getInstance().settheSecretNumber(intSecretNumber);
	}
	
	@Override
	protected void entertheNumber() {
		Log.d("GfxTutorial::entertheNumber", edtNewNumber.getText().toString() + " " + Integer.toString(arrShots[intTutorialCounter]));
		if(numberConsole.btnEnter.getText().toString() == "Cont."){
			removeDialog(R.id.display_start);
			showDialog(R.id.display_start);
			numberConsole.btnEnter.setText(R.string.enter);
			numberConsole.btnEnter.setEnabled(false);
			for(int i = 1; i < 10; i++){
				numberConsole.arrbtnNumbers[i].setEnabled(true);
			}
			return;
		}
		if(Integer.parseInt(edtNewNumber.getText().toString()) != arrShots[intTutorialCounter]){
			Toast.makeText(this, "Please enter " + Integer.toString(arrShots[intTutorialCounter]), Toast.LENGTH_SHORT).show();			
		} else{
			if(Integer.parseInt(edtNewNumber.getText().toString()) != intSecretNumber){
				numberConsole.btnEnter.setText("Cont.");
				Toast.makeText(this, "Press continue", Toast.LENGTH_SHORT).show();			
			}
			super.entertheNumber();
			if(numberConsole.btnEnter.getText().toString() == "Cont."){
				numberConsole.btnEnter.setEnabled(true);
				for(int i = 1; i < 10; i++){
					numberConsole.arrbtnNumbers[i].setEnabled(false);
				}
			}
		}
	}

	@Override
	protected void resumetheGame() {
	}

	@Override
	protected void savetheGame() {
	}
	
	@Override
	protected void gameSucceded() {
		super.gameSucceded();
		showDialog(R.id.display_success);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//super.onCreateOptionsMenu(menu);
		menu.add(0, EXIT_ID, 0, R.string.exit);
		return true;
	}
	
	@Override
	protected Dialog onCreateDialog(int id) {
		Log.d("GfxMasterShot::onCreateDialog", "enter");
		String message = new String();
		switch (id) {
		case R.id.display_start:
			message = formatTutorial();
			intTutorialCounter++;
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.droid_small)
			.setTitle(R.string.menu_tutorial)
			.setMessage(message)
			.setPositiveButton(R.string.alert_dialog_continue, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
					switch(intTutorialCounter - 1){
					case 1:
					case 3:
					case 6:
					case 8:
					case 9:
					case 11:
					case 12:
					case 14:
					case 15:
						removeDialog(R.id.display_start);
						showDialog(R.id.display_start);
						break;
					default:
						toasttheNumber();
						break;
					}
				}
			})
			.create();
		case R.id.display_success:
			Log.d("GfxMasterShot::onCreateDialog", "Succcess " +  Integer.toString(SMBGuesstheNumber.nCurrentLevel));
			message = formatTutorial();
			return new AlertDialog.Builder(this)
			.setIcon(R.drawable.smiley_happy)
			.setTitle(R.string.dialog_success)
			.setMessage(message)
			.setPositiveButton(R.string.OK, new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,	int whichButton) {
				}
			})
			.create();
		}
		return super.onCreateDialog(id);
	}
	
	private String formatTutorial(){
		String message = new String();
		switch(intTutorialCounter){
		case 0:
			message = "Welcome!\n" +
					"This is a simple mind game. You need to find the three digit number in my mind. Let\'s take " +
					"a simple guess. Press 1, 2, and 3. Then press Enter";
			break;
		case 1:
			message = "The result is +0 -0\n" +
					"You couldn\'t find any digit in the actual number. This might seem bad. But it is not.";
			break;
		case 2:
			message = "Now we know that 1,2 and 3 are not in the actual number. So, we narrowed down the possible " +
			"digits. Let\'s take another guess 456";
			break;

		case 3:
			message = "The result is +1 -0\n" +
					"We catched one of the digits in the actual number and it is in the right place. But" +
					", unfortunately, we don\'t know which digit is this. So, we need to dig out more." +
					" Nothing is easy, right?";
			break;
		case 4:
			message = "Let's find which digit is in the actual number. " +
			"We know that 1, 2 and 3 are not in the actual number. So, Let\'s say 412. " +
			"If we find a number, we can, definitely, say that it is 4.";
			break;
		case 5:
			message = "The result is +0 -0\n" +
					"We have no luck. But we know that 4 is not the digit we are looking for. " +
					"Let's say 152";
			break;
		case 6:
			message = "The result is +1 -0\n" +
					"Nice! 5 is the digit we are looking for and we know that it is in the right place.\n" +
					"That\'s very good but not enough.";
			break;
		case 7:
			message = "We still have two more digits to find. However; " +
			"we know that these 2 digits are not one of 1-6 digits. So, let\'s continue with 789.";
			break;
		case 8:
			message = "The result is +1 -1\n" +
					"So, one thing to mention is the other two digits are in 7-9. So, we can exclude 0 as " +
					"well even though we didn't try it."; 
			break;
		case 9:
			message = "Consider the signs at that point. We saw + before and " +
			"we said the digit is in the same place. Thus, - means there is a matching digit in your " +
			"number but it is not in the same place.";
			break;
		case 10:
			message = "So, we may find which two digits are in the actual number in the right place. Let's start " +
			"by adding the digit we already know it is in the actual number: 5 and add two other digits " +
			"from 7-9. So, enter 759";
			break;
		case 11:
			message = "The result is +2 -0\n" +
					"We are very near to find the number. We know two digits from the actual number and their " +
					"places are right. But still we don't know whether it is 7 or 9.";
			break;
		case 12:
			message =  "On the other hand, we" +
			" definitely know that 8 is in the actual number and it\'s the digit where its place is" +
			" not right."; 
			break;
		case 13:
			message = "So, let\'s put 8 in other place instead of 7 or 9. So, we will know the right " +
			"place of 8 and moreover which digit (7 or 9) is in the actual number. The next guess is 859.";
			break;
		case 14:
			message = "The result is +1 -1\n" +
					"OK. this is not the number we are looking for. But we are definitely sure that 9 is not " +
					"in the actual number. Because, if it was, we would have one more + digit. but it is only +1 " +
					"which is referring to 5.";
			break;
		case 15: 
			message = "The good news, we know that 7 is in the actual number and in the " +
			"right place. On the other hand, -1 indicates that 8 is still not in the right place. So, only " +
			"one place is left we didn\'t try."; 
			break;
		case 16:
			message = "We are definitely sure that the actual number is" +
			" 758. Enter the number.";
			break;
		case 17:
			message = "The result is +3 -0\n" +
					"You found the number in 8 shots. Now you can master the game by" +
					" challenging yourself with the minimum shots and time. It is strongly advised to take " +
					"quick shots and then when you feel comfortable to take Master Shots for an exciting " +
					"challenge.";
		}
		return message;
		
	}
	
	private void toasttheNumber(){
		Toast.makeText(this, "Please enter " + Integer.toString(arrShots[intTutorialCounter]), Toast.LENGTH_SHORT).show();			
	}
}
